﻿Imports Microsoft.Win32

Public Class IESearchEngines
    Implements IEnumerable(Of SearchEngine)

    Public Structure SearchEngine
        Private m_DisplayName As String
        Public Property DisplayName() As String
            Get
                Return m_DisplayName
            End Get
            Set(ByVal value As String)
                m_DisplayName = value
            End Set
        End Property

        Private m_SearchString As String
        Public Property SearchString() As String
            Get
                Return m_SearchString
            End Get
            Set(ByVal value As String)
                m_SearchString = value
            End Set
        End Property

        Private m_isDefault As Boolean
        Public Property isDefault() As Boolean
            Get
                Return m_isDefault
            End Get
            Set(ByVal value As Boolean)
                m_isDefault = value
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return m_DisplayName.ToString
        End Function
    End Structure

    Private m_engineList As List(Of SearchEngine) = New List(Of SearchEngine)


    Private m_defaultEngine As SearchEngine
    Public ReadOnly Property DefaultEngine() As SearchEngine
        Get
            Return m_defaultEngine
        End Get
    End Property


    Public Sub New()
        'Gather engines from registry
        GatherEnginesFromRegistry()

        'Set default engine
        m_defaultEngine = GetDefaultEngineFromList()
    End Sub

    Private Sub GatherEnginesFromRegistry()
        'Clear list
        m_engineList.Clear()

        Dim regKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Internet Explorer\SearchScopes", False)
        'Gather Default key
        Dim defaultKey As String = regKey.GetValue("DefaultScope")

        'Gather all search engines
        For Each subKey As String In regKey.GetSubKeyNames
            Dim engine As New SearchEngine
            Dim engineKey As RegistryKey = regKey.OpenSubKey(subKey)
            If engineKey.GetValue("") = String.Empty Then
                engine.DisplayName = engineKey.GetValue("DisplayName")
            Else
                engine.DisplayName = engineKey.GetValue("")
            End If
            engine.SearchString = engineKey.GetValue("URL")
            If (subKey = defaultKey) Then
                engine.isDefault = True
            Else
                engine.isDefault = False
            End If
            m_engineList.Add(engine)
            engineKey.Close()
        Next

        regKey.Close()
    End Sub

    Private Function GetDefaultEngineFromList() As SearchEngine
        Dim defaultEngine As New SearchEngine
        For Each engine As SearchEngine In m_engineList
            If engine.isDefault Then
                defaultEngine = engine
            End If
        Next
        Return defaultEngine
    End Function

    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of SearchEngine) Implements System.Collections.Generic.IEnumerable(Of SearchEngine).GetEnumerator
        Return m_engineList.GetEnumerator
    End Function

    Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        Return m_engineList.GetEnumerator
    End Function
End Class
